/**
 * @class Spark.QueryForm
 * @extends Spark.ViewComponent
 * <p>QueryForm组件.
 * @author njq.niu@hand-china.com
 * @constructor 
 */
$S.QueryForm = Ext.extend($S.ViewComponent,{
    isExpanded : false,
    initComponent : function(cfg){
        $S.QueryForm.superclass.initComponent.call(this, cfg);
        this.child = cfg.child||[];
        this.inner = this.wrap.child('div.s_qf_w');
        this.body = Ext.get(this.id+'_body');
        this.shadow = this.wrap.child('div[stype=shadow]');
        this.menuBtn = $(cfg.queryButton);
    },
    initEvents : function(){
        $S.QueryForm.superclass.initEvents.call(this);
        this.addEvents(
        /**
         * @event expand
         * expand事件.
         * @param {Component} this 当前组件.
         */
        'expand',
        /**
         * @event collapse
         * collapse事件.
         * @param {Component} this 当前组件.
         */
        'collapse');
        this.processListener('on');
    },
    processListener: function(ou){
        $S.QueryForm.superclass.processListener.call(this,ou);
        if(this.menuBtn){
            this.menuBtn[ou]('expand',this.expand,this);
            this.menuBtn[ou]('collapse',this.collapse,this);
        }
    },
    triggerBlur : function(e,t){
        if(!this.isEventFromComponent(t)){    
            this.collapse();
            this.menuBtn.trbtn.removeClass('up');
            this.menuBtn.trbtn.addClass('down');  
        }
    },
    isEventFromComponent:function(el){
        var v = $S.QueryForm.superclass.isEventFromComponent.call(this,el);
        if(!v)
        Ext.each(this.child,function(id){
            var cmp = $(id);
            if(cmp && cmp.isEventFromComponent){
                if(cmp.isEventFromComponent(el)){
                    v = true;
                    return true;
                }
            }
        })
        return v;
    },
    /**
     * 折叠弹出面板
     */
    collapse : function(){
        if(!this.isExpanded) return;
        Ext.get(document.documentElement).un("mousedown", this.triggerBlur, this);
        this.body.setStyle('display', 'none');
        this.inner.removeClass('s_qf_w_s');
        this.inner.setStyle({'position':'','z-index':'','top':'0px','left':'0px','border':'none'});
        this.shadow.moveTo(-1000,-1000);
        this.isExpanded = false;
    },
    /**
     * 展开弹出面板
     */
    expand : function(){
        Ext.get(document.documentElement).un("mousedown", this.triggerBlur, this).on("mousedown", this.triggerBlur, this);
        var xy = this.wrap.getXY();
        this.body.setStyle('display', '');
        this.inner.addClass('s_qf_w_s');
        this.inner.setStyle({'position':'absolute','z-index':'9990','border':'1px solid #ccc'});
        this.inner.moveTo(xy[0]-1, xy[1]-1);
        this.shadow.setWidth(this.inner.getWidth());
        this.shadow.setHeight(this.inner.getHeight());
        this.shadow.moveTo(xy[0]-1, xy[1]-1);
        this.isExpanded = true;
    }
})